﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace ex5_1
{
    public partial class Form1 : Form
    {
        public static string name;
        public static string passwd;
        public Form1()
        {
            InitializeComponent();
        }
        Boolean printError(TextBox name, TextBox pwd)
        {
            if (name.TextLength == 0 || pwd.TextLength == 0)
            {
                MessageBox.Show("用户名或密码不能为空");
                return true;
            }
            else
            {
                return false;
            }
        }
        void connectSqlfinduser()
        {
            SqlConnection conn = new SqlConnection("Data Source=192.168.30.1;Initial Catalog=STU;Persist Security Info=True;User ID=tt;Password=123456");
            conn.Open();

            // 编辑 SQL 语句
            SqlCommand cmd = new SqlCommand("select username,password from [stuinfo] where username='" + textBox1.Text + "'and password='" + textBox2.Text + "'", conn);

            // 遍历 user 表
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            int n = da.Fill(ds);
            if (n != 0)
            {
                name = textBox1.Text;
                passwd = textBox2.Text;
                //MessageBox.Show("登录成功");
                Form3 fm = new Form3();
                fm.Show();
                this.Hide();

            }
            else
            {
                MessageBox.Show("用户名或密码错误");
            }
            conn.Close();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }


        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!printError(textBox1,textBox2))
            {
                connectSqlfinduser();
            }


        }
        private void button2_Click(object sender, EventArgs e)
        {
            Form2 fm = new Form2();
            fm.Show();

        }





        //show input error message



        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
